package structures;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.net.URL;

/**
 * Created with IntelliJ IDEA.
 * User: Claudio "Dna" Bonesana
 * Date: 19.04.13
 * Time: 21:41
 */
public class City {

    /**
     * Label for data update. This class will update automatically the relative labels.
     */
    protected JLabel licon;
    protected JLabel lname;
    protected JLabel ltemphumy;
    protected JLabel ldescr;

    protected JPanel panel;

    protected String byWhat;

    protected String name;
    protected String observationTime;
    protected String weatherDescr;
    protected String weatherImg;
    protected String temperature;
    protected String windStrength;
    protected String windDirection;
    protected String precipitation;
    protected String humidity;
    protected String pressure;
    protected String comment;


    public City() {

    }

    public JLabel getLicon() {
        return licon;
    }

    public void setLicon(JLabel licon) {
        this.licon = licon;
    }

    public JLabel getLname() {
        return lname;
    }

    public void setLname(JLabel lname) {
        this.lname = lname;
    }

    public JLabel getLtemphumy() {
        return ltemphumy;
    }

    public void setLtemphumy(JLabel ltemphumy) {
        this.ltemphumy = ltemphumy;
    }

    public JPanel getPanel() {
        return panel;
    }

    public void setPanel(JPanel panel) {
        this.panel = panel;
    }

    public JLabel getLdescr() {
        return ldescr;
    }

    public void setLdescr(JLabel ldescr) {
        this.ldescr = ldescr;
    }

    public String getByWhat() {
        return byWhat;
    }

    public void setByWhat(String byWhat) {
        this.byWhat = byWhat;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getObservationTime() {
        return observationTime;
    }

    public void setObservationTime(String observationTime) {
        this.observationTime = observationTime;
    }

    public String getWeatherDescr() {
        return weatherDescr;
    }

    public void setWeatherDescr(String weatherDescr) {
        this.weatherDescr = weatherDescr;
        // if there is a label, update the label
        if (ldescr != null)
            if (weatherDescr != null)
                ldescr.setText(this.weatherDescr);
            else
                ldescr.setText("");
    }

    public String getWeatherImg() {
        return weatherImg;
    }

    public void setWeatherImg(String weatherImg) {
        this.weatherImg = weatherImg;
        // if there is an image, update the image
        if (licon != null){
            try {
                URL url = new URL(this.weatherImg);
                Image image = ImageIO.read(url);
                licon.setText("");
                licon.setIcon(new ImageIcon(image));
            } catch (IOException e) {
                licon.setText("...");
            }
        }
    }

    public String getTemperature() {
        return temperature;
    }

    public void setTemperature(String temperature) {
        this.temperature = temperature;
        // if there is a label, update it
        if (ltemphumy != null){
            if (this.temperature != null && this.humidity != null)
                ltemphumy.setText(this.temperature + "°C " + this.humidity + "%");
            else
                ltemphumy.setText("");
        }
    }

    public String getWindStrength() {
        return windStrength;
    }

    public void setWindStrength(String windStrength) {
        this.windStrength = windStrength;
    }

    public String getWindDirection() {
        return windDirection;
    }

    public void setWindDirection(String windDirection) {
        this.windDirection = windDirection;
    }

    public String getPrecipitation() {
        return precipitation;
    }

    public void setPrecipitation(String precipitation) {
        this.precipitation = precipitation;
    }

    public String getHumidity() {
        return humidity;
    }

    public void setHumidity(String humidity) {
        this.humidity = humidity;
        // if there is a label, update
        if (ltemphumy != null){
            ltemphumy.setText(this.temperature + "°C " + this.humidity + "%");
        }
    }

    public String getPressure() {
        return pressure;
    }

    public void setPressure(String pressure) {
        this.pressure = pressure;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    @Override
    public String toString() {
        return "City{" +
                "name='" + name + '\'' +
                ", observationTime='" + observationTime + '\'' +
                ", weatherDescr='" + weatherDescr + '\'' +
                ", weatherImg='" + weatherImg + '\'' +
                ", temperature='" + temperature + '\'' +
                ", windStrength='" + windStrength + '\'' +
                ", windDirection='" + windDirection + '\'' +
                ", precipitation='" + precipitation + '\'' +
                ", humidity='" + humidity + '\'' +
                ", pressure='" + pressure + '\'' +
                '}';
    }

    public void setParameter(String param, String content) {
        if (param.equals("Humidity"))
            setHumidity(content);
        if (param.equals("ObservationTime"))
            setObservationTime(content);
        if (param.equals("Precipitation"))
            setPrecipitation(content);
        if (param.equals("Pressure"))
            setPressure(content);
        if (param.equals("Temperature"))
            setTemperature(content);
        if (param.equals("Weather"))
            setWeatherDescr(content);
        if (param.equals("WeatherImg"))
            setWeatherImg(content);
        if (param.equals("WindDirection"))
            setWindDirection(content);
        if (param.equals("WindStrength"))
            setWindStrength(content);
    }
}
